﻿using Autofac.Core;
using EMS.system.Entityies.WarehouseManagement.WarehouseSettingsModel;
using EMS.system.IEntityAppService.WarehouseManagement.WarehouseSetup;
using EMS.system.IEntityAppService.WarehouseManagement.WarehouseSetup.WarehouseSetupDTO;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace EMS.system.Web.Controllers.WarehouseManagement.WarehouseSetup
{
    public class WarehouseSetupController : Controller
    {
        private readonly IWarehouseSetupAppService _warehouseSetupAppService;

        public WarehouseSetupController(IWarehouseSetupAppService warehouseSetupAppService)
        {
            _warehouseSetupAppService = warehouseSetupAppService;
        }

        public IActionResult WarehouseSetupList()
        {
            return View();
        }

        public IActionResult Create()
        {
            return View();
        }

        public IActionResult Edit()
        {
            return View();
        }

    

        [HttpGet]
        public JsonResult GetWarehouseSetup(string? code, string? name, int page = 1, int limit = 2)
        {
            var list = _warehouseSetupAppService.GetWarehouseSetup(code, name, page, limit);
            return Json(new { data = list.Queryable, count = list.RowCount, code = "0", msg = "" });
        }

        [HttpPost]
        public async Task<int> AddWarehouseSetup(WarehouseSetupInsertDTO dto)
        {
            var result = await _warehouseSetupAppService.AddWarehouseSetup(dto);
            return result;
        }

        

        [HttpPost]
        public int DeleteWarehouseSetup(int id)
        {
            var result = _warehouseSetupAppService.DeleteWarehouseSetup(id);
            return result;
        }

        [HttpGet]
        public  JsonResult GetWarehouseSetupById(int id)
        {
            var result =  _warehouseSetupAppService.GetWarehouseSetupById(id);
            return Json(new { data = result, code = "0", msg = "" });
        }
        [HttpPost]
        public async Task<int> UpdateWarehouseSetup(WarehouseSetupUpdateDTO dto)
        {
            var list =  await _warehouseSetupAppService.UpdateWarehouseSetup(dto);
            return list;
        }

        [HttpPost]
        public int DeleAll(string id)
        {
            int res = 0;
            foreach (var ids in id.Split(","))
            {
                res = _warehouseSetupAppService.DeleteWarehouseSetup(int.Parse(ids));
            }
            return res;
        }
    }
}
